﻿namespace symlib;

public static class Strchr {
    public static Boolean IsAlpha(Char c) {
        return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
    }
    public static Boolean IsBinDigit(Char c) {
        return c == '0' || c == '1';
    }
    public static Boolean IsTerDigit(Char c) {
        return c == '0' || c == '1' || c == '2';
    }
    public static Boolean IsOctDigit(Char c) {
        return c >= '0' && c <= '7';
    }
    public static Boolean IsDecDigit(Char c) {
        return c >= '0' && c <= '9';
    }
    public static Boolean IsHexDigit(Char c) {
        return c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F';
    }
    public static Boolean IsAlphaNumeric(Char c) {
        return IsAlpha(c) || IsDecDigit(c);
    }
    public static Boolean IsNameStart(Char c) {
        return IsAlpha(c) || c == '_';
    }
    public static Boolean IsNamePart(Char c) {
        return IsAlphaNumeric(c) || c == '_';
    }
    public static Boolean IsSpace(Char c) {
        return c == ' ' || c == '\t' || c == '\n' || c == '\r';
    }
    public static Boolean IsAscii(Char c) {
        return c >= 0 && c <= 127;
    }
    public static Boolean IsUnicode(Char c) {
        return c >= 0 && c <= 65535;
    }
    public static Boolean IsControl(Char c) {
        if(c >= 0 && c <= 31) return true;
        if(c >= 127 && c <= 159) return true;
        return false;   
    }
    public static Boolean IsPrint(Char c) {
        return !IsControl(c);
    }
    public static Boolean IsGraph(Char c) {
        return !IsControl(c) && !IsSpace(c);
    }
    public static Boolean IsLower(Char c) {
        return c >= 'a' && c <= 'z';
    }
    public static Boolean IsUpper(Char c) {
        return c >= 'A' && c <= 'Z';
    }
    public static Char ToLower(Char c) {
        if (IsUpper(c)) return (Char)(c + 32);
        return c;
    }
    public static Char ToUpper(Char c) {
        if (IsLower(c)) return (Char)(c - 32);
        return c;
    }
    
    public static Boolean IsAlpha(String s) {
        if (s.Length == 0) return false;
        foreach (Char c in s)
            if (!IsAlpha(c)) return false;
        return true;
    }
    public static Boolean IsAlphaNumeric(String s) {
        if (s.Length == 0) return false;
        foreach (Char c in s)
            if (!IsAlphaNumeric(c)) return false;
        return true;
    }
    public static Boolean IsName(String s) {
        if (s.Length == 0) return false;
        if (!IsNameStart(s[0])) return false;
        for (int i = 1; i < s.Length; i++)
            if (!IsNamePart(s[i])) return false;
        return true;
    }
    public static Boolean IsBinary(String s) {
        if (s.Length == 0) return false;
        foreach (Char c in s)
            if (!IsBinDigit(c)) return false;
        return true;
    }
    public static Boolean IsTernary(String s) {
        if (s.Length == 0) return false;
        foreach (Char c in s)
            if (!IsTerDigit(c)) return false;
        return true;
    }
    public static Boolean IsOctal(String s) {
        if (s.Length == 0) return false;
        foreach (Char c in s)
            if (!IsOctDigit(c)) return false;
        return true;
    }
    public static Boolean IsDecimal(String s) {
        if (s.Length == 0) return false;
        foreach (Char c in s)
            if (!IsDecDigit(c)) return false;
        return true;
    }
    public static Boolean IsHexadecimal(String s) {
        if (s.Length == 0) return false;
        foreach (Char c in s)
            if (!IsHexDigit(c)) return false;
        return true;
    }
    public static Boolean IsSpace(String s) {
        foreach (Char c in s)
            if (!IsSpace(c)) return false;
        return true;
    }
    public static Boolean IsAscii(String s) {
        foreach (Char c in s)
            if (!IsAscii(c)) return false;
        return true;
    }
    public static Boolean IsUnicode(String s) {
        foreach (Char c in s)
            if (!IsUnicode(c)) return false;
        return true;
    }
    public static Boolean IsControl(String s) {
        foreach (Char c in s)
            if (!IsControl(c)) return false;
        return true;
    }
    public static Boolean IsPrint(String s) {
        foreach (Char c in s)
            if (!IsPrint(c)) return false;
        return true;
    }
    public static Boolean IsGraph(String s) {
        if (s.Length == 0) return false;
        foreach (Char c in s)
            if (!IsGraph(c)) return false;
        return true;
    }
    public static Boolean IsLower(String s) {
        foreach (Char c in s)
            if (!IsLower(c)) return false;
        return true;
    }
    public static Boolean IsUpper(String s) {
        foreach (Char c in s)
            if (!IsUpper(c)) return false;
        return true;
    }
    public static String ToUpper(String s) {
        System.Text.StringBuilder res = new System.Text.StringBuilder(s);
        for (int i = 0; i < res.Length; i++) res[i] = ToUpper(res[i]);
        return res.ToString();
    }
    public static String ToLower(String s) {
        System.Text.StringBuilder res = new System.Text.StringBuilder(s);
        for (int i = 0; i < res.Length; i++) res[i] = ToLower(res[i]);
        return res.ToString();
    }
    public static String ToString(Char c) {
        return c.ToString();
    }
}
